import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      // {
      //   path: '',
      //   components: {
      //     main: () => import('@/views/main.vue'),
      //     aside: () => import('@/views/aside.vue')
      //   }
      // },
      // {
      //   path: 'container',
      //   component: () => import('@/views/container.vue')
      // }
      {
        path: '/ceshi',
        name: 'ceshi',
        component: () => import(/* webpackChunkName: "about" */ '@/views/ceshi/index.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',

    component: () => import(/* webpackChunkName: "about" */ '@/views/login/index.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  linkActiveClass: 'active',
  routes
})

// GOOD
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('user')
  if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
  else next()
})

export default router
